﻿Imports System.Data.SqlClient

Public Class Frm_idafat_9da2i

    Public dv As Dossier
    Public avo As Khibra
    Public dep As Khibra_khabir
    Dim deKhab As DictionaryEntry
    Dim ListViewItem1 As System.Windows.Forms.ListViewItem

    Public Sub remplirList()

        List_tous.Items.Clear()
        Dim Str As String = ""
        For Each dosskhab As Khibra_khabir In avo.List_khabir
            For Each khab As Khabir In Partager.moi.List_khabir
                If dosskhab.Id_Khabir = khab.Id Then

                    ListViewItem1 = New System.Windows.Forms.ListViewItem(New String() {"", khab.Nom, khab.Prenom}, -1)
                    'il est indispensable d'ajouter chekHand false et true
                    Partager.checkHand = False
                    If dosskhab.nomme Then
                        ListViewItem1.Checked = True
                    Else
                        ListViewItem1.Checked = False
                    End If
                    Me.List_tous.Items.AddRange(New System.Windows.Forms.ListViewItem() {ListViewItem1})
                    Partager.checkHand = True
                End If
            Next
        Next

    End Sub

    Public Sub remplirkhabir(ByVal comb As ComboBox)
        Partager.moi.remplir_list_khabir()

        Dim htClient As New Hashtable()
        Dim str As String = ""
        For Each khab As Khabir In Partager.moi.List_khabir
            str = khab.Nom & "  " & khab.Prenom
            htClient.Add(khab.Id, str)
        Next

        For Each ImageType As DictionaryEntry In htClient
            comb.Items.Add(ImageType)
        Next
        comb.DisplayMember = "value"
        comb.ValueMember = "key"
        If comb.Items.Count > 0 Then
            comb.SelectedIndex = 0
        End If
    End Sub

    Private Sub btn_parc_khabir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_parc_khabir.Click
        Me.Enabled = False
        Frm_Rech_khabir.Show()
        Partager.frmMoule = New Form
        Partager.frmMoule = Me
        Partager.type = "Frm_idafat_9da2i"
    End Sub

    Private Sub Frm_idafat_9da2i_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        List_tous.Columns(0).DisplayIndex = List_tous.Columns.Count - 1
        Partager.close()

        Partager.nonInitial = New ArrayList
        Partager.nonInitial.Add("Comb_marji3ona")
        Partager.remplirDossier(Comb_marji3ona)

        remplirkhabir(Comb_khabir)
    End Sub

    Private Sub Comb_khibra_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_khibra.SelectedIndexChanged
        Try
            For Each d As Khibra In dv.List_khibra
                If d.Num = Comb_khibra.Text Then
                    avo = d
                    avo.remplir_list_khabir()

                End If
            Next
            remplirList()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub List_tous_ItemChecked(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles List_tous.ItemChecked
        'Dim dossAvo As dossAvoBur = dv.List_avocat_bureau(List_mowakilin.SelectedIndices.Item(0))
        If Partager.checkHand Then
            Dim i As Integer = 0
            For Each item1 As ListViewItem In List_tous.Items
                dep = New Khibra_khabir
                dep = avo.List_khabir(i)
                dep.nomme = item1.Checked
                avo.modifierkhabNomme(dep)
                i += 1
            Next
        End If
    End Sub

    Private Sub Btn_ajout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajout.Click
        If Comb_marji3ona.SelectedIndex < 0 Then
            MsgBox("اختر المرجع أولا", MsgBoxStyle.Critical, "خطأ في الإضافة")
        Else
            Try
                If Comb_khibra.Text <> "" And Comb_khabir.Text <> "" Then
                    If MsgBox("هل تريد فعلا إضافة هذا الخبير لهذه الخبرة ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة") = MsgBoxResult.Yes Then
                        Partager.close()
                        deKhab = DirectCast(Comb_khabir.SelectedItem, DictionaryEntry)
                        Dim idKhab As Integer = CInt(deKhab.Key)
                        dep = New Khibra_khabir(idKhab, avo.Id, "1")
                        avo.Ajouter_khabir(idKhab)
                        remplirList()
                        MsgBox("تمت إضافة الخبير للخبرة بنجاح", MsgBoxStyle.Information, "نجاح")
                    End If
                End If
            Catch sqlEx As SqlException
                If sqlEx.Message.Contains("PK_Khibra_khabir") Then
                    MsgBox("عفوا، هذا الخبير تمت إضافته لهذه الخبرة من قبل", MsgBoxStyle.Critical, "خطأ")
                Else
                    MsgBox(sqlEx.Message)
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If
    End Sub

    Private Sub Btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp.Click
        Try
            If List_tous.SelectedIndices.Count > 0 Then
                If MsgBox("هل تريد فعلا حذف هذا الخبير ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف الخبير") = MsgBoxResult.Yes Then
                    dep = avo.List_khabir(List_tous.SelectedIndices.Item(0))
                    avo.suprimmer_khabir(dep)
                    remplirList()
                    MsgBox("عملية الحذف تمت بنجاح", MsgBoxStyle.Information, "نجاح")
                End If
            Else
                MsgBox("اختر خبير من القائمة", MsgBoxStyle.Information, "خطأ")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Comb_marji3ona_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_marji3ona.SelectedIndexChanged
        Partager.vider(Me, Partager.nonInitial)
        Try
            Partager.deClt = DirectCast(Comb_marji3ona.SelectedItem, DictionaryEntry)
            For Each d As Dossier In Partager.moi.List_info_fichier
                If d.id = CInt(Partager.deClt.Key) Then
                    dv = d
                    dv.remplir_List_khibra()
                End If
            Next

            List_tous.Items.Clear()
            Comb_khibra.Items.Clear()
            For Each khib As Khibra In dv.List_khibra
                Comb_khibra.Items.Add(khib.Num)
            Next
            If Comb_khibra.Items.Count > 0 Then
                Comb_khibra.SelectedIndex = 0
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_Rech_Doss_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Rech_Doss.Click

        Me.Enabled = False
        Partager.frmafect = Me
        Frm_Recherche.Show()
        Partager.type = Me.Name

    End Sub
End Class